from django.shortcuts import render
from django.http import HttpResponse
import json
from tower.models import *
from asset.models import *
# Create your views here.


def asset_repairs(request):
    if request.method == 'GET':
        return HttpResponse('没有访问权限')
    elif request.method == 'POST':
        info = dict(request.POST)
        try:
            t1 = Tower.objects.get(tower_name = info['tower_name'][0])
        except Tower.DoesNotExist:
            return HttpResponse('没有这个楼')
        except KeyError:
            return HttpResponse('输入错误')
        else:
            try:
                f1 = t1.floor_set.get(floor_name = info['floor_name'][0])
            except Floor.DoesNotExist:
                return HttpResponse('没有该楼层')
            except KeyError:
                return HttpResponse('输入错误')
            else:
                try:
                    r1 = f1.room_set.get(room_num = info['room_name'][0])
                except Room.DoesNotExist:
                    return HttpResponse('没有这个房间')
                except KeyError:
                    return HttpResponse('输入错误')
                else:
                    try:
                        a1 = Asset.objects.get(asset_name = info['asset_name'][0])
                    except Asset.DoesNotExist:
                        return HttpResponse('没有这个资产')
                    except KeyError:
                        return HttpResponse('输入错误')
                    else:
                        RepairsRecord(as_name = info['asset_name']
                        as_num = info['num'],
                        bx_user= info['person_name'],
                        bx_room = info['room_name'],
                        remark = info['notes']).save()





